'use strict';
const Sequelize = require('sequelize');

const DbProvider = require('../db/db_provider');

const BS_TREE_STORE = Symbol('BSTree#STORE');
const BS_TREE_GET_ALL = Symbol('BSTreeStore#GET#ALL');

/**
 * bstree
 * [{id,name}]
 */
class BSTreeStore {
  constructor() {
    this.db = DbProvider.getNewDb();
  }

  async fetch() {
    if (!this[BS_TREE_STORE]) {
      this[BS_TREE_STORE] = await this[BS_TREE_GET_ALL]();
    }
    return this[BS_TREE_STORE] || [];
  }

  list() {
    return this.fetch();
  }

  /**
 * 查询所有的rcms_Categories
 * @return {Promise<Array>} 数据行
 */
  [BS_TREE_GET_ALL]() {
    return this.db.query('SELECT id,name FROM bs_tree', {
      raw: true,
      type: Sequelize.QueryTypes.SELECT,
    });
  }
}

module.exports = new BSTreeStore();
